package com.hevs.samplewebapp.server;

import java.io.BufferedReader;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;

import com.hevs.samplewebapp.server.entities.MedicalRecord;
import com.hevs.samplewebapp.server.json.ObjectMapperFactory;
import com.hevs.samplewebapp.server.service.MedicalRecordService;
import com.hevs.samplewebapp.server.service.UserService;

/**
 * 
 * @author Arnaud Durand creation : 20.03.12
 * 
 *         Goal: RESTful service for the Medical Records
 * 
 */
public class MedicalRecordDeleteServlet extends SessionAuthHttpServlet {

	public MedicalRecordDeleteServlet() {
		super(1);
	}
	
	public static UserService userService;
	{
		userService = new UserService();
	};

	public static final String queryMail = "usermail";
	private static final long serialVersionUID = 870866277965499216L;

	public static MedicalRecordService medicalRecordService;
	{
		medicalRecordService = new MedicalRecordService();
	};

	@Override
	protected void doSecurePost(HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {

		BufferedReader reader = req.getReader();
		StringBuilder sb = new StringBuilder();
		String line = reader.readLine();
		while (line != null) {
			sb.append(line + "\n");
			line = reader.readLine();
		}
		reader.close();

		ObjectMapper mapper = ObjectMapperFactory.get();
		MedicalRecord medicalRecord = mapper.readValue(sb.toString(),
				MedicalRecord.class);

		if (medicalRecord == null || medicalRecord.getKey() == null) {
			resp.sendError(400);
			return;
		}
		medicalRecordService.removeMedicalRecord(medicalRecord);

	}


}
